package zozopo.warehouse.operations;

import zozopo.operations.comm.OperatingService;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;


public class SupplierOS extends OperatingService{
	
	
	public SupplierOS(Connection conn){
		super(conn);
	}
	
	public SupplierOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<SupplierTB> selectListBySql(String sql){
		List<SupplierTB> supplierList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					supplierList = new ArrayList<SupplierTB>();
					while(resultSet.next()){
						SupplierTB supplier = new SupplierTB();
						
						supplier.setId_supplier(resultSet.getString(SupplierTB.ID_SUPPLIER));
						supplier.setAddress_supplier(resultSet.getString(SupplierTB.ADDRESS_SUPPLIER));
						supplier.setDate_supplier(resultSet.getString(SupplierTB.DATE_SUPPLIER));
						supplier.setManager_supplier(resultSet.getString(SupplierTB.MANAGER_SUPPLIER));
						supplier.setMobile_supplier(resultSet.getString(SupplierTB.MOBILE_SUPPLIER));
						supplier.setName_supplier(resultSet.getString(SupplierTB.NAME_SUPPLIER));
						supplier.setPhone_supplier(resultSet.getString(SupplierTB.PHONE_SUPPLIER));
						supplier.setQq_supplier(resultSet.getString(SupplierTB.QQ_SUPPLIER));
						supplier.setRemark_supplier(resultSet.getString(SupplierTB.REMARK_SUPPLIER));
						
						supplier.setUseremail_supplier(resultSet.getString(SupplierTB.USERMAIL_SUPPLIER));
						supplier.setUsername_supplier(resultSet.getString(SupplierTB.USERNAME_SUPPLIER));
						
						supplierList.add(supplier);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return supplierList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertSupplier(SupplierTB supplier){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(SupplierTB.ADDRESS_SUPPLIER, supplier.getAddress_supplier());
		//insertMap.put(SupplersTB.DATE_SUPPLIER, goods.getUnit_goods());
		insertMap.put(SupplierTB.MANAGER_SUPPLIER, supplier.getManager_supplier());
		insertMap.put(SupplierTB.MOBILE_SUPPLIER, supplier.getMobile_supplier());
		insertMap.put(SupplierTB.NAME_SUPPLIER, supplier.getName_supplier());
		insertMap.put(SupplierTB.PHONE_SUPPLIER, supplier.getPhone_supplier());
		insertMap.put(SupplierTB.QQ_SUPPLIER, supplier.getQq_supplier());
		
		insertMap.put(SupplierTB.REMARK_SUPPLIER, supplier.getRemark_supplier());
		insertMap.put(SupplierTB.USERMAIL_SUPPLIER, supplier.getUseremail_supplier());
		insertMap.put(SupplierTB.USERNAME_SUPPLIER, supplier.getUsername_supplier());
		
		return sqlMachine.insertRow(SupplierTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadteSupplier(SupplierTB supplier){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(SupplierTB.ADDRESS_SUPPLIER, supplier.getAddress_supplier());
		//insertMap.put(supplierTB.DATE_supplier, goods.getUnit_goods());
		updateMap.put(SupplierTB.MANAGER_SUPPLIER, supplier.getManager_supplier());
		updateMap.put(SupplierTB.MOBILE_SUPPLIER, supplier.getMobile_supplier());
		updateMap.put(SupplierTB.NAME_SUPPLIER, supplier.getName_supplier());
		updateMap.put(SupplierTB.PHONE_SUPPLIER, supplier.getPhone_supplier());
		updateMap.put(SupplierTB.QQ_SUPPLIER, supplier.getQq_supplier());
		
		updateMap.put(SupplierTB.REMARK_SUPPLIER, supplier.getRemark_supplier());
		updateMap.put(SupplierTB.USERMAIL_SUPPLIER, supplier.getUseremail_supplier());
		updateMap.put(SupplierTB.USERNAME_SUPPLIER, supplier.getUsername_supplier());
		
		
		
		
		return sqlMachine.updateRow(SupplierTB.TABLE_NAME, updateMap, SupplierTB.ID_SUPPLIER, supplier.getId_supplier());
	}
	
	
	public List<SupplierTB> selectSupplier(int beginPageNum,int pageSize){
		
		List<SupplierTB> supplierList = null;
		String sql = "SELECT * FROM " + SupplierTB.TABLE_NAME  +" limit "+beginPageNum+","+pageSize; 
		supplierList = selectListBySql(sql);
		
		return supplierList;
	}
	
	public SupplierTB getSupplierById(String id){
		String sql = "SELECT * FROM " + SupplierTB.TABLE_NAME + " where " + SupplierTB.ID_SUPPLIER + "=" + id;
		SupplierTB supplier = new SupplierTB();
		List<SupplierTB> supplierList = selectListBySql(sql);
		supplier = supplierList.get(0);
		return supplier;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(SupplierTB.ID_SUPPLIER, SupplierTB.TABLE_NAME, SupplierTB.USERMAIL_SUPPLIER, email);
		return maxId;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + SupplierTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<SupplierTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+SupplierTB.TABLE_NAME+" WHERE  "
		+SupplierTB.ID_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.ADDRESS_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.NAME_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.MANAGER_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectListBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + SupplierTB.TABLE_NAME +" WHERE  "
		+SupplierTB.ID_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.ADDRESS_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.NAME_SUPPLIER+" LIKE '%"+searchStr+"%'"
		+" OR "+SupplierTB.MANAGER_SUPPLIER+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
}
